package com.wollo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wollo.domain.Check;
import com.wollo.domain.User;
import com.wollo.dto.StudentCheckWorkDTO;
import com.wollo.dto.StudentPageDTO;
import com.wollo.mapper.StudentCheckMapper;
import com.wollo.mapper.UserMapper;
import com.wollo.service.StudentCheckWorkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * @author 栾钰凇
 * Date  2024/9/14 18:28
 */
@Service
public class StudentCheckWorkServiceImpl implements StudentCheckWorkService {
    @Autowired
    private StudentCheckMapper studentCheckMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 分页查询该学生的考勤信息
     *
     * @param studentPageDTO
     * @return
     */
    @Override
    public PageInfo<Check> getAllCheckInfo(StudentPageDTO studentPageDTO) {
        User user = userMapper.selectUserInfoByUserNameMapper(studentPageDTO.getUsername());
        PageHelper.startPage(studentPageDTO.getPageNum(), studentPageDTO.getPageSize());
        //处理按时间查询的条件
        if (studentPageDTO.getCheckTimes() != null && !studentPageDTO.getCheckTimes().isEmpty()) {
            //获取日期时间,将日期时间转为日期并加一天
            LocalDate beginTime = studentPageDTO.getCheckTimes().get(0).toLocalDate().plusDays(1);
            LocalDate endTime = studentPageDTO.getCheckTimes().get(1).toLocalDate().plusDays(1);
            List<Check> checks = studentCheckMapper.getAllCheckInfo(user.getUid(), beginTime, endTime);
            return new PageInfo<>(checks);
        }else {
            if(studentPageDTO.getPageNum() == 1){
                List<Check> checks = studentCheckMapper.getAllCheckInfo(user.getUid(), null, null);
                return new PageInfo<>(checks);
            }
            List<Check> checks = studentCheckMapper.getAllCheckInfo(user.getUid(), null, null);
            return new PageInfo<>(checks);
        }
    }

    /**
     * 添加打卡信息
     *
     * @param studentCheckWorkDTO
     * @return
     */
    @Override
    public boolean addCheckInfo(StudentCheckWorkDTO studentCheckWorkDTO) {
        User user = userMapper.selectUserInfoByUserNameMapper(studentCheckWorkDTO.getUsername());

        LocalTime localTime = studentCheckWorkDTO.getCheckTime().toLocalTime();
        String checkResult = "0";
        String checkType = "";
        LocalTime morningStandardTime = LocalTime.of(8, 30);
        LocalTime afternoonStandardTime = LocalTime.of(13, 30);
        LocalTime eveningStandardTime = LocalTime.of(19, 0);
        //上午打卡时间段 1 上午 2 下午 3 晚上
        if(localTime.isAfter(LocalTime.of(7,0)) && localTime.isBefore(LocalTime.of(12,0))){
            checkType = "1";
            if(localTime.isAfter(morningStandardTime)){
                //0 正常 1 迟到
                checkResult = "1";
            }
            //下午打卡时间段
        }else if(localTime.isAfter(LocalTime.of(12,0)) && localTime.isBefore(LocalTime.of(17,20))){
            checkType = "2";
            if(localTime.isAfter(afternoonStandardTime)){
                checkResult = "1";
            }
            //晚自习打卡时间段
        }else if(localTime.isAfter(LocalTime.of(18,0)) && localTime.isBefore(LocalTime.of(21,0))){
            checkType = "3";
            if(localTime.isAfter(eveningStandardTime)){
                checkResult = "1";
            }
            //未到打卡时间
        }else {
            return false;
        }
        Check check = Check.builder()
                .uid(user.getUid())
                .checktime(studentCheckWorkDTO.getCheckTime())
                .checkstatus(checkType)
                .checkresult(checkResult)
                .build();
        studentCheckMapper.addCheckInfo(check);
        return true;
    }
}
